/**
 * 
 */
package dms.presentation.action.sys;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dms.dto.ClassesDTO;
import dms.model.Classes;
import dms.model.Faculty;
import dms.model.util.PageIterator;
import dms.service.SchoolService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.PageUtil;

/**
 * @author leejohn
 *
 */
public class ClassesAction extends DispatchAction {
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long facultyId = null;
		PageIterator<Classes> classes = null;
		Faculty faculty = null;
		int start;
		int count;
		int[] params = null;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		
		try {
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			facultyId = Long.valueOf(request.getParameter("facultyId"));
			faculty = schoolService.findFacultyById(facultyId);
			
			classes = schoolService.findClassesByFaculty(faculty, start, count);
			
			request.setAttribute("faculty", faculty);
			request.setAttribute("classes", classes);
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
		
		return mapping.findForward("list");
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		long facultyId;
		Faculty faculty = null;
		String redirectURL = null;
		try {
			facultyId = Long.valueOf(request.getParameter("facultyId"));
			faculty = schoolService.findFacultyById(facultyId);
			redirectURL = "faculty.do?method=list&schoolId=" + faculty.getSchool().getId();
			request.setAttribute("faculty", faculty);
		} catch (Exception e) {
			log.error(e.getMessage());
			response.sendRedirect(redirectURL);
			return null;
		}
		
		return mapping.findForward("addForm");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ClassesDTO dto = (ClassesDTO) form;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		String redirectURL = null;

		try {
			schoolService.createClasses(dto);
			
			redirectURL = "classes.do?method=list&facultyId=" + dto.getFaculty();
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.ERROR);
		}
		
		response.sendRedirect(redirectURL);
		return null;
	}
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long classesId = null;
		Classes classes = null;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		
		try {
			classesId = Long.valueOf(request.getParameter("id"));
			classes = schoolService.findClassesById(classesId);
			
			request.setAttribute("classes", classes);
		} catch (Exception e) {
			log.error(e.getMessage());
			/*redirectURL = "classes.do?method=list&facultyId=" + classes.getFaculty().getId();
			response.sendRedirect(redirectURL);*/
			
			return mapping.findForward(Constant.ERROR);
		}
		
		return mapping.findForward("updateForm");
	}
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ClassesDTO dto = (ClassesDTO) form;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		String redirectURL = null;
		
		try {
			schoolService.updateClasses(dto);
			redirectURL = "classes.do?method=list&facultyId=" + dto.getFaculty();
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.ERROR);
		}
		
		response.sendRedirect(redirectURL);
		return null;

	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Long classesId = null;
		SchoolService schoolService = (SchoolService) ContextUtil.getContext().getBean("schoolService");
		String redirectURL = null;
		Classes classes = null;
		
		try {
			classesId = Long.valueOf(request.getParameter("id"));
			classes = schoolService.findClassesById(classesId);
			schoolService.deleteClassesById(classesId);
			redirectURL = "classes.do?method=list&facultyId=" + classes.getFaculty().getId(); 
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.ERROR);
			
		}
		response.sendRedirect(redirectURL);
		return null;
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward(Constant.ERROR);
	}
}
